import os

def rename_images_in_folder(folder_path):
    for root, _, files in os.walk(folder_path):
        for file in files:
            # Check if the file is an image
            if file.endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')):
                # Extract the Chinese part of the filename
                new_name = ''.join([char for char in file if '\u4e00' <= char <= '\u9fff']) + ".jpg"
                
                # Rename the file if the new name is not empty
                if new_name:
                    old_path = os.path.join(root, file)
                    new_path = os.path.join(root, new_name)
                    os.rename(old_path, new_path)
                    print(f"Renamed: {old_path} -> {new_path}")

def fix_missing_extensions(folder_path):
    for root, _, files in os.walk(folder_path):
        for file in files:
            # Check if the file has no extension
            if '.' not in file:
                old_path = os.path.join(root, file)
                new_path = f"{old_path}.jpg"
                os.rename(old_path, new_path)
                print(f"Fixed: {old_path} -> {new_path}")

if __name__ == "__main__":
    folders = [
        "地图",
        "求生者（大）",
        "求生者（小）",
        "监管主动技",
        "监管者（大）",
        "监管者（小）"
    ]

    base_path = os.path.dirname(os.path.abspath(__file__))

    for folder in folders:
        folder_path = os.path.join(base_path, "随机赛专用图鉴", folder)
        if os.path.exists(folder_path):
            rename_images_in_folder(folder_path)
            fix_missing_extensions(folder_path)
        else:
            print(f"Folder not found: {folder_path}")